package com.xltt.vehicleControl.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.xltt.vehicleControl.R;
import com.xltt.vehicleControl.activity.LauncherActivity;
import com.xltt.vehicleControl.base.BaseFragment;
import com.xltt.vehicleControl.bean.AdditionalVehicleStatus;
import com.xltt.vehicleControl.bean.BasicVehicleStatus;
import com.xltt.vehicleControl.bean.Capability;
import com.xltt.vehicleControl.bean.MaintenanceStatus;
import com.xltt.vehicleControl.bean.RunningStatus;
import com.xltt.vehicleControl.bean.VehicleStatus;
import com.xltt.vehicleControl.contants.Constants;
import com.xltt.vehicleControl.util.Logger;
import com.xltt.vehicleControl.util.SharedPreferencesUtils;
import com.xltt.vehicleControl.util.ToastUtil;

import java.util.List;

import butterknife.BindView;

public class VehicleInfoFragment extends BaseFragment implements LauncherActivity.VehicleCallBack{

    @BindView(R.id.vehicle_id)//车牌号
    TextView mVehicleIdView;
    @BindView(R.id.location_txt)//位置
    TextView mLocationView;
    @BindView(R.id.speed_txt)//速度
    TextView mSpeedView;
    @BindView(R.id.km_txt)//剩余可续航里程
    TextView mKmView;
    @BindView(R.id.youhao_txt)//油耗
    TextView mYouhaoView;
    @BindView(R.id.burn_txt)//油耗
    TextView mBurnView;
    @BindView(R.id.refresh_view)//刷新控件
    SwipeRefreshLayout mRefreshView;

    @Override
    public View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.vehicle_info_fragment, container, false);
    }

    @Override
    public void onResume() {
        super.onResume();
        initVehicleStatus();
        Logger.d("onResume()");
    }

    @Override
    public void onPause() {
        super.onPause();
        Logger.d("onPause()");
    }

    private void initVehicleStatus(){
        //获取最新数据
        LauncherActivity activity = ((LauncherActivity)getActivity());
        //初始化下拉刷新view
        mRefreshView.setColorSchemeResources(R.color.colorPrimary);
        mRefreshView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                String userId = ((LauncherActivity)getActivity()).LoginInvalidation();
                activity.initBindVehicleList(userId);
                new Handler().postDelayed(mRunnable,6000);
            }
        });
        //activity.initVehicleStatus();
        activity.registerObserver(this);
    }

    private Runnable mRunnable = new Runnable(){
        @Override
        public void run() {
            if(mRefreshView != null && mRefreshView.isRefreshing()){
                mRefreshView.setRefreshing(false);
            }
        }
    };

    @Override
    public void onRefreshInfo(VehicleStatus vehicleStatus, String plateNo) {
        Logger.d("VehicleInfoFragment revice observer");
        BasicVehicleStatus basicVehicleStatus = vehicleStatus.getBasicVehicleStatus();
        if(basicVehicleStatus == null){
            ToastUtil.show(getActivity(),"basicVehicleStatus data null");
            return;
        }
        if(mVehicleIdView == null){
            ToastUtil.show(getActivity(),"init car plate view error");
            return;
        }

        MaintenanceStatus statue = vehicleStatus.getAdditionalVehicleStatus().getMaintenanceStatus();
        AdditionalVehicleStatus mAdditionalVehicleStatus = vehicleStatus.getAdditionalVehicleStatus();
        mLocationView.setText(statue == null ? "---" : statue.getDistanceToService() + "km");//剩余保养公里数/位置
        mVehicleIdView.setText(plateNo);//车牌
        mBurnView.setText(mAdditionalVehicleStatus.getRunningStatus().getFuelLevel()+"L");//油量
        mKmView.setText(basicVehicleStatus.getDistanceToEmpty()+"km");//里程
        mSpeedView.setText(mAdditionalVehicleStatus.getRunningStatus().getAvgSpeed()+"km/h");//平均速度
        RunningStatus runningStatus = mAdditionalVehicleStatus.getRunningStatus();
        if(Constants.DEBUG){
            SharedPreferencesUtils.setParam(mContext, Constants.VEHICLE_CONSUME_INFO,runningStatus.getAveFuelConsumption()+"");
        }else {
            Settings.Global.putString(mContext.getContentResolver(), Constants.VEHICLE_CONSUME_INFO, runningStatus.getAveFuelConsumption()+"");
        }
        mYouhaoView.setText(runningStatus.getAveFuelConsumption() + "L");//油耗
        mRefreshView.setRefreshing(false);//取消刷新UI
    }

    @Override
    public void onRemoteControl(int result) {

    }

    @Override
    public void onFunctionList(List<Capability> capabilityList) {

    }

}
